#include "glare.d3d11.Texture.h"
#include "glare.d3d11.FormatMapping.h"
#include "glare.d3d11.Driver.h"
#include "glare.d3d11.GpuResourceFactory.h"

namespace Glare {

	D3D11Texture::D3D11Texture(D3D11GpuResourceFactory* factory, const TextureDescription& description)
		: Texture(description)
		, _factory(factory)
		, _d3d_texture(NULL)
	{
		D3D11Driver* d3d11_rs = static_cast<D3D11Driver*>(factory->GetDriver());

		D3D11_TEXTURE2D_DESC d3d11_tex_desc;
		// resource options
		d3d11_tex_desc.Usage			= (D3D11_USAGE)description.UsageFlags;
		d3d11_tex_desc.BindFlags		= description.BindFlags;
		d3d11_tex_desc.CPUAccessFlags	= description.CpuAccessFlags;
		d3d11_tex_desc.MiscFlags		= description.MiscFlags;

		// texture options
		d3d11_tex_desc.ArraySize			= description.ArraySize;
		d3d11_tex_desc.Format				= D3D11FormatMapping::MapFormat(description.Format);
		d3d11_tex_desc.Height				= description.Height;
		d3d11_tex_desc.MipLevels			= description.MipLevels;
		d3d11_tex_desc.SampleDesc.Count		= description.MsaaLevel;
		d3d11_tex_desc.SampleDesc.Quality	= description.MsaaQuality;
		d3d11_tex_desc.Width				= description.Width;

		GLARE_THROW(d3d11_rs->GetD3DDevice()->CreateTexture2D(&d3d11_tex_desc, NULL, &_d3d_texture), "Error while creating D3D texture");
	}

	D3D11Texture::~D3D11Texture()
	{
		GLARE_SAFE_RELEASE(_d3d_texture, "Error while releasing D3D texture");
	}

	GpuResource::Interface* D3D11Texture::_GetInterface()
	{
		return this;
	}

	GpuResourceFactory* D3D11Texture::GetFactory() const
	{
		return _factory;
	}

	ID3D11Texture2D* D3D11Texture::GetD3DTexture() const
	{
		return _d3d_texture;
	}

	D3D11GpuResourceFactory* D3D11Texture::GetD3DFactory() const
	{
		return _factory;
	}

	ID3D11Resource* D3D11Texture::GetD3DResource()
	{
		return _d3d_texture;
	}
}
